package com.hll.api.controller.kaola;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.Collect;
import com.hll.api.entity.User;
import com.hll.api.service.ICollectService;
import com.hll.api.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author 何龙
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/api/kaola/collect")
public class CollectController {
    @Resource
    ICollectService service;
    @Resource
    UserService userService;

    /**
     * @api {post} /api/kaola/collect/add/{id} 增加收藏
     * @apiVersion 1.0.0
     * @apiName collectAdd
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/collect/add/10000
     */
    @PostMapping("/add/{id}")
    public RespMessage add(@PathVariable String id, HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        Collect one = service.getOne( wrapper );
        if (one != null) {
            return RespMessage.error( "您已经收藏过了" );
        }

        Collect collect = new Collect();
        collect.setDate( new Date() );
        collect.setUserId( user.getId().intValue() );
        collect.setGoodsId( id );
        service.save( collect );
        return RespMessage.success( "收藏成功" );
    }

    /**
     * @api {delete} /api/kaola/collect/del/{id} 删除收藏
     * @apiVersion 1.0.0
     * @apiName collectDel
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/collect/del/10000
     */
    @DeleteMapping("del/{id}")
    public RespMessage del(@PathVariable String id, HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        service.remove( wrapper );
        return RespMessage.success( "移除收藏成功" );
    }

    /**
     * @api {get} /api/kaola/collect/get 收藏列表
     * @apiVersion 1.0.0
     * @apiName collectGet
     * @apiGroup kaola
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/collect/get
     */
    @GetMapping("/get")
    public RespMessage get(HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).orderByDesc( "date" );
        List<Collect> list = service.list( wrapper );
        return RespMessage.success( list, "" );
    }


    /**
     * @api {get} /api/kaola/collect/get/{id} 获取商品是否收藏
     * @apiVersion 1.0.0
     * @apiName collectGetT
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/collect/get/10000
     */
    @GetMapping("/get/{id}")
    public RespMessage getCollect(@PathVariable String id, HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        Collect one = service.getOne( wrapper );
        boolean c = true;
        if (one == null) {
            c = false;
        }
        return RespMessage.success( c, "" );

    }
}
